package com.hxx.day8;

public class TestDemo4 {
}

/**
 * 单例模式
 * volatile:不具有原子性，具有可见性
 * 懒汉式：自旋锁
 */
class Singleton{
    private Singleton(){}
    //volatile 关键字，防止指令重排序
    private volatile static Singleton singleton = null;
    public static Singleton getInstance(){
        //双检锁
        synchronized (Singleton.class){
            if(singleton == null){
                singleton = new Singleton();
            }
        }
        return singleton;
    }
   /* public synchronized static Singleton getInstance(){
        if(singleton == null){
            singleton = new Singleton();
        }
        return singleton;
    }*/
}
